package com.isecommerce.application.datawizard;

import java.io.PrintWriter;

import com.isecommerce.framework.util.Config;
import com.isecommerce.framework.util.LogWriter;
import com.isecommerce.application.datawizard.entity.*;

/**
 * Standard Format Meta Info Manager<br><br>
 *
 * This class cache the standard format meta info from 'conf.proeprties'.
 * 
 *
 * @version 1.2.5
 * @author Leslie Chen
 */
// History

public class StandardFormatInfoManager {
    
    protected StandardFormatInfoEntity info;
    
    private LogWriter logger;
            
    /**
     * Initializing manager from application configuration file.
     *
     */
    public void init(Config config){
    	
    	// Init logger
    	this.logger = LogWriter.getLogWriterInstance("DATA_WIZARD", LogWriter.INFO, new PrintWriter(System.out));
    	this.logger.setOwner(this.getClass().getSimpleName());
    	            	
    	try{
        	// Get CSV file info
           	info = new StandardFormatInfoEntity(config.getValue("stdformat.column.delimiter"),
 		               			                config.getValue("stdformat.aggregate.mark"),
                                                config.getValue("stdformat.filename.postfix"),
                               this.getLogLevel(config.getValue("stdformat.log.level")));
           	           	
           	this.logger.log("[OK] Standard Format Meta Info registered.", LogWriter.INFO);
           	
        }catch(Exception e){
        	this.logger.log("Standard Format Meta Info failed to register", LogWriter.ERROR);
        	this.logger.log(e, "Properties not found", LogWriter.ERROR);
        }                	
        
    	this.logger.log("Standard Format Meta Info registered successfully.", LogWriter.INFO);
    }
    
    /**
     * Get file info by given csv alias name.
     *
     */
    public StandardFormatInfoEntity getCSVfileInfo(){
        return info;
    }
    
    private int getLogLevel(String logLevelCode){
    	if(logLevelCode.equals("DEBUG")) return LogWriter.DEBUG;
    	else if(logLevelCode.equals("INFO")) return LogWriter.INFO;
    	else if(logLevelCode.equals("ERROR")) return LogWriter.ERROR;
    	else return LogWriter.NONE;
    }
    
}
